﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI.WebControls;
using blackhouse.BlackApart.Business.Apartments;
using blackhouse.BlackApart.Business;
using System.ComponentModel;

namespace blackhouse.BlackApart.Web.Modules {
    public class ApartmentGroupLiteral : Literal {

        #region Fields

        #endregion

        #region Properties

        public string PropertyKey { get; set; }

        [DefaultValue("{0}")]
        public string TextFormat { get; set; }

        [DefaultValue(false)]
        public bool FormatToHtml { get; set; }

        #endregion

        #region Constructors

        public ApartmentGroupLiteral() {
            this.TextFormat = "{0}";
        }

        #endregion

        #region Methods

        protected override void OnLoad(EventArgs e) {
            base.OnInit(e);

            this.PopulateText();
        }

        private void PopulateText() {
            if (!this.TextFormat.Contains("{0"))
                this.TextFormat = "{0}";

            DisplayApartmentTypeState state = DisplayApartmentTypeState.GetCurrentState();

            if (state != null && state.ApartmentType != null) {
                object propertyVal = state.ApartmentType.GetProperty(this.PropertyKey);
                if (propertyVal != null)
                    this.Text = string.Format(this.TextFormat, propertyVal);
            }
            if (this.FormatToHtml)
                this.Text = this.FormatHtml(this.Text);
            this.Visible = !string.IsNullOrEmpty(this.Text);
        }

        private string FormatHtml(string text) {
            return text.ReplaceEx("\r\n", "<br />");
        }

        #endregion

    }
}